package cn.ronghuanet.permission.controller;

import cn.ronghuanet.annotation.RonghuaPermission;
import cn.ronghuanet.permission.domain.Role;
import cn.ronghuanet.permission.dto.RolePermissionDTO;
import cn.ronghuanet.permission.query.RoleQuery;
import cn.ronghuanet.permission.service.IRoleService;
import cn.ronghuanet.basic.utils.AjaxResult;
import cn.ronghuanet.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RonghuaPermission(name = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 增删改操作只需要告诉前端,操作成功还是失败就可以了
     * @param role
     * @return
     */
    // 新增/修改
    @RonghuaPermission(name = "新增或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if(Objects.isNull(role.getId())){   // 如果ID为空,说明是新增操作
                roleService.add(role);
            }else{
                // 修改操作
                roleService.update(role);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }

    // 根据ID删除单条
    @RonghuaPermission(name = "根据ID删除")
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            roleService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    // 批量删除
    @RonghuaPermission(name = "批量删除")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            roleService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败!"+e.getMessage());
        }
    }

    // 根据ID查询
    @RonghuaPermission(name = "根据ID查询")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id")Long id){
        try {
            Role role = roleService.getById(id);
            return AjaxResult.me().setDataObj(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    // 查询所有
    @RonghuaPermission(name = "查询所有")
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Role> roles = roleService.getAll();
            return AjaxResult.me().setDataObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    /**
     * 分页+高级查询
     * 入参: RoleQuery -> currentPage pageSize keyword
     * 返参: AjaxResult -> success message dataObj:PageList(total,rows)
     */
    @RonghuaPermission(name = "分页高级查询")
    @PostMapping
    public AjaxResult listPage(@RequestBody RoleQuery query){
        try {
            PageList<Role> pageList = roleService.listPage(query);
            return AjaxResult.me().setDataObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    @RonghuaPermission(name = "设置权限")
    @PutMapping("/permission")
    public AjaxResult saveRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.saveRolePermission(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    @RonghuaPermission(name = "获取角色的权限")
    @GetMapping("/permissionsns/{roleId}")
    public AjaxResult getRolePermissionSns(@PathVariable("roleId")Long roleId){
        try {
            List<String> sns = roleService.getRolePermissionSns(roleId);
            return AjaxResult.me().setDataObj(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取角色的权限失败!"+e.getMessage());
        }
    }
}
